package cn.com.bluemoon.daps.sync.entity;

import cn.com.bluemoon.daps.common.constant.DapConstant;
import cn.com.bluemoon.daps.common.domain.BaseModel;
import cn.com.bluemoon.daps.common.enums.SyncStatus;
import cn.com.bluemoon.daps.common.enums.SyncToEnv;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * <p>
 * 发布同步单号数据维护
 * </p>
 *
 * @author admin
 * @since 2021-06-17
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors
@ApiModel(value = "DapSyncProdEnvTask", description = "发布同步单号数据维护")
public class DapSyncProdEnvTask extends BaseModel<DapSyncProdEnvTask> {

    public static final String COMPARE_VERSION_KEY = "DapSyncProdEnvTask#Id";
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "申请发布中的变更类型, 1-3表示新增，修改，删除")
    @Deprecated // 本字段来源于申请发布数据中，表示的维度紊乱废除（单号对应表多个变更无法归为一种变更类型）
    private Integer changeType = null;

    @ApiModelProperty(value = "申请发布中的变更单号")
    private String changeOrder;

    @ApiModelProperty(value = "申请发布的申请人")
    private String applicationBy;

    @ApiModelProperty(value = "申请发布的申请时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private LocalDateTime applicationTime;

    @ApiModelProperty(value = "申请发布通过的审核人")
    private String verifyBy;

    @ApiModelProperty(value = "申请发布的变更描述")
    private String changeDesc;

    @ApiModelProperty(value = "申请发布的模型id")
    private String modelId;

    @ApiModelProperty(value = "申请发布的数据源信息")
    private String dataSourceId;

    @ApiModelProperty(value = "预发布同步的执行人")
    private String preProdSyncBy;

    @ApiModelProperty(value = "预发布时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private LocalDateTime preProdSyncTime;

    @ApiModelProperty(value = "预生产同步状态，0：未同步、1：同步中、2：已同步、3：同步失败，若模型数据源中不存在该环境则不进行同步")
    @EnumValue
    private SyncStatus preProdStatus;

    @ApiModelProperty(value = "生产同步状态，0：未同步、1：同步中、2：已同步、3：同步失败，若模型数据源中不存在该环境则不进行同步")
    @EnumValue
    private SyncStatus prodStatus;

    @ApiModelProperty(value = "发布时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private LocalDateTime prodSyncTime;

    @ApiModelProperty(value = "发布同步的执行人")
    private String prodSyncBy;

    /**
     * 用于判断沙箱环境失败情况下，发布同步预览的表脚本都归为未执行
     */
    @ApiModelProperty(value = "是否为执行沙箱环境失败，0否 1是，默认为0")
    private Boolean sandboxSyncFail;

    @ApiModelProperty(hidden = true)
    public void setSyncStatus(SyncStatus syncStatus, SyncToEnv syncToEnv) {
        if (syncToEnv == null) {
            return;
        }
        if (syncStatus == null) {
            return;
        }
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            setProdStatus(syncStatus);
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            setPreProdStatus(syncStatus);
        }
    }

    @ApiModelProperty(hidden = true)
    public SyncStatus getSyncStatus(SyncToEnv syncToEnv) {
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            return this.prodStatus;
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            return this.preProdStatus;
        }
        return null;
    }

    @ApiModelProperty(hidden = true)
    public void setSyncTime(LocalDateTime syncTime, SyncToEnv syncToEnv) {
        if (syncToEnv == null) {
            return;
        }
        if (syncTime == null) {
            return;
        }
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            setProdSyncTime(syncTime);
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            setPreProdSyncTime(syncTime);
        }
    }

    @ApiModelProperty(hidden = true)
    public LocalDateTime getSyncTime(SyncToEnv syncToEnv) {
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            return this.prodSyncTime;
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            return this.preProdSyncTime;
        }
        return null;
    }

    @ApiModelProperty(hidden = true)
    public void setSyncBy(String syncBy, SyncToEnv syncToEnv) {
        if (syncToEnv == null) {
            return;
        }
        if (syncBy == null) {
            return;
        }
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            setProdSyncBy(syncBy);
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            setPreProdSyncBy(syncBy);
        }
    }

    @ApiModelProperty(hidden = true)
    public String getSyncBy(SyncToEnv syncToEnv) {
        if (SyncToEnv.PROD.equals(syncToEnv)) {
            return this.prodSyncBy;
        }
        if (SyncToEnv.PRE_PROD.equals(syncToEnv)) {
            return this.preProdSyncBy;
        }
        return null;
    }

    /**
     * 获取执行人邮件
     *
     * @return str
     */
    public String getSyncExecByEmail(SyncToEnv syncToEnv) {
        return DapConstant.formatEmail(this.getSyncBy(syncToEnv), true);
    }

    /**
     * 优先拿生产同步人
     *
     * @return str
     */
    public String getSyncByOrNull() {
        if (this.prodSyncBy != null) {
            return this.prodSyncBy;
        }
        if (this.preProdSyncBy != null) {
            return this.preProdSyncBy;
        }
        return null;
    }

    /**
     * 优先拿生产同步时间
     *
     * @return time
     */
    public LocalDateTime getSyncTimeOrNull() {
        if (this.prodSyncTime != null) {
            return this.prodSyncTime;
        }
        if (this.preProdSyncTime != null) {
            return this.preProdSyncTime;
        }
        return null;
    }
}
